import { _decorator, Component, instantiate, Node, UITransform, Vec3 } from 'cc';
import { ResMgr } from '../Mgr/ResMgr';
import { AssetList, Global } from '../Global';
import { CubeInfo, SingleCubeBg } from './SingleCubeBg';
import { Tools } from '../Tools';
import { UIMgr } from '../Mgr/UIMgr';
import { GameScene } from './GameScene';
import { ControlCubeLoad } from './ControlCubeLoad';
import { ModelLoad } from './ModelLoad';
import { AudioMgr, SoundName } from '../Mgr/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('CubeBgLoad')
/**
 * 方块背景层
 */
export class CubeBgLoad extends Component {

    //方块大小
    private CubeSize: { width: number, height: number } = { width: 50, height: 50 }

    //方块间距
    private CubeDis: { hor: number, ver: number } = { hor: 12, ver: 12 };

    //方块数组
    public CubeArray: Node[][] = [];

    //操作层
    @property(Node)
    public ControlCubeLoad: Node = null;

    //加分得砖块
    public AllAddScoreCubes: Node[] = [];
    //相邻得砖块
    public NeightSameCubes: Node[] = [];

    /**
     * 背景初始化
     */
    public InitBg() {
        this.node.angle = 45;

        //整体宽度
        let all_width: number = Global.HorNum * (this.CubeSize.width + this.CubeDis.hor);
        let all_height: number = Global.VerNum * (this.CubeSize.height + this.CubeDis.ver);

        for (let i = 0; i < Global.HorNum; i++) {
            let ver_arr: Node[] = [];
            for (let j = 0; j < Global.VerNum; j++) {
                let cube: Node = instantiate(ResMgr.Instance.Prefabs[AssetList.Prefabs.GameEleBg]);
                this.node.addChild(cube);
                cube.getComponent(SingleCubeBg).InitCubeInfor({ hor: i, ver: j, ele: -1 })

                cube.setPosition(new Vec3(
                    -all_width / 2 + (i + 0.5) * (this.CubeSize.width + this.CubeDis.hor),
                    -all_height / 2 + (j + 0.5) * (this.CubeSize.height + this.CubeDis.ver),
                ))

                // let ss: Vec3 = this.ControlCubeLoad.getComponent(UITransform).convertToNodeSpaceAR(cube.getWorldPosition());
                // cube.getComponent(SingleCubeBg).TestContent.string = ss.x.toFixed(0) + "," + ss.y.toFixed(0)


                ver_arr.push(cube);
            };
            this.CubeArray.push(ver_arr);
        }

    }

    /**
     * 判断是否落在方块上
     */
    public CheckPutCube(_touchpos: Vec3): Node {
        for (let i = 0; i < Global.HorNum; i++) {
            for (let j = 0; j < Global.VerNum; j++) {
                let cube_pos: Vec3 = this.CubeArray[i][j].getWorldPosition();
                if (Tools.getDistance(cube_pos.x, cube_pos.y, _touchpos.x, _touchpos.y) < 30 &&
                    this.CubeArray[i][j].getComponent(SingleCubeBg).CubeInfo.ele == -1) {
                    //判断是否可以消失
                    return this.CubeArray[i][j];
                }
            }
        }
        return null;
    }

    /**
     * 判断可以消失得方块
     */
    public CheckDis() {
        console.log("检测消失");
        UIMgr.Instance.ShowTop(AssetList.Top.NoTouch);
        this.AllAddScoreCubes = [];

        for (let i = 0; i < Global.HorNum; i++) {
            for (let j = 0; j < Global.VerNum; j++) {
                if (this.CubeArray[i][j].getComponent(SingleCubeBg).CubeInfo.ele != -1) {
                    this.NeightSameCubes = [];
                    this.CheckNeightSameSprite(this.CubeArray[i][j], i, j);
                    if (this.NeightSameCubes.length > 2) {
                        for (let single_cube of this.NeightSameCubes) {
                            if (this.AllAddScoreCubes.indexOf(single_cube) == -1) {
                                this.AllAddScoreCubes.push(single_cube);
                            }
                        }
                    }
                }
            }
        }

        //如果有可以消除的
        if (this.AllAddScoreCubes.length > 0) {
            AudioMgr.instance.PlayEffect(SoundName.Clear);
            let score_num: number = this.AllAddScoreCubes.length;

            for (let single_cube of this.AllAddScoreCubes) {
                single_cube.getComponent(SingleCubeBg).AddScore(score_num);
            }

            GameScene.Instance.ModelLoad.getComponent(ModelLoad).ClearSuccessUpdatePromptCube(
                this.AllAddScoreCubes[0].getComponent(SingleCubeBg).CubeInfo.ele,
                score_num
            )

            this.scheduleOnce(() => {//延时动画点击
                UIMgr.Instance.HideTop(AssetList.Top.NoTouch);
                GameScene.Instance.ControlCubeLoad.getComponent(ControlCubeLoad).CheckHavePutDown();
            }, 0.5)


        } else {
            UIMgr.Instance.HideTop(AssetList.Top.NoTouch);
            GameScene.Instance.ControlCubeLoad.getComponent(ControlCubeLoad).CheckHavePutDown();
        }


    }

    /**
    * 检测相邻得精灵
    */
    private CheckNeightSameSprite(_sprite: Node, _hor: number, _ver: number): void {
        if (this.NeightSameCubes.indexOf(_sprite) != -1) return;

        this.NeightSameCubes.push(_sprite);

        let cube_info: CubeInfo = _sprite.getComponent(SingleCubeBg).CubeInfo;

        //左侧判断
        if (_hor > 0 && this.CubeArray[_hor - 1][_ver].getComponent(SingleCubeBg).CubeInfo.ele != -1 && this.CubeArray[_hor - 1][_ver].getComponent(SingleCubeBg).CubeInfo.ele == cube_info.ele) {
            this.CheckNeightSameSprite(this.CubeArray[_hor - 1][_ver], _hor - 1, _ver);
        }

        //右侧判断
        if (_hor < Global.HorNum - 1 && this.CubeArray[_hor + 1][_ver].getComponent(SingleCubeBg).CubeInfo.ele != -1 && this.CubeArray[_hor + 1][_ver].getComponent(SingleCubeBg).CubeInfo.ele == cube_info.ele) {
            this.CheckNeightSameSprite(this.CubeArray[_hor + 1][_ver], _hor + 1, _ver);
        }

        //上方
        if (_ver > 0 && this.CubeArray[_hor][_ver - 1].getComponent(SingleCubeBg).CubeInfo.ele != -1 && this.CubeArray[_hor][_ver - 1].getComponent(SingleCubeBg).CubeInfo.ele == cube_info.ele) {
            this.CheckNeightSameSprite(this.CubeArray[_hor][_ver - 1], _hor, _ver - 1);
        }

        //下方
        if (_ver < Global.VerNum - 1 && this.CubeArray[_hor][_ver + 1].getComponent(SingleCubeBg).CubeInfo.ele != -1 && this.CubeArray[_hor][_ver + 1].getComponent(SingleCubeBg).CubeInfo.ele == cube_info.ele) {
            this.CheckNeightSameSprite(this.CubeArray[_hor][_ver + 1], _hor, _ver + 1);
        }

    }

    /**
     * 返回这个位置是否有砖块
     */
    public ReturnIsHaveCube(_pos: Vec3): boolean {
        for (let i = 0; i < Global.HorNum; i++) {
            for (let j = 0; j < Global.VerNum; j++) {
                let cube_pos: Vec3 = this.CubeArray[i][j].getWorldPosition();
                if (Tools.getDistance(cube_pos.x, cube_pos.y, _pos.x, _pos.y) < 30 &&
                    this.CubeArray[i][j].getComponent(SingleCubeBg).CubeInfo.ele == -1) {
                    //判断是否可以消失
                    return true;
                }
            }
        }
        return false;
    }

}


